package findEnsemble;

import genetic_algorithm.Chromosome;
import genetic_algorithm.Mutation;

import java.util.List;

import utils.RandomGenerator;

public class FindEnsembleMutation implements Mutation {

	public static final int ALL_DIGITS = 0;
	public static final int SINGLE_DIGIT = 1;
	
	int mlpListSize;
	int mode;
	
	public FindEnsembleMutation(int mlpListSize, int mode) {
		this.mlpListSize = mlpListSize;
		this.mode = mode;
	}
	
	@Override
	public void mutate(List<Chromosome> chromosomes, double rate) {

		for(Chromosome c :chromosomes) {
			if(RandomGenerator.nextDouble() < rate) {

				// mutate chromosome according to specified
				switch (mode) {
				case ALL_DIGITS: {
					
					allDigits(c);
					break;
				}
				case SINGLE_DIGIT: {
					
					singleDigit(c);
					break;
				}

				default:
					break;
				}
			}
		}
	}
	
	private void allDigits(Chromosome chromosome) {
		
		// choose a new network for each digit
		for (int digit = 0 ; digit < FindEnsembleMain.NUM_CLASSES ; ++digit) {
			
			// set a new index randomly
			singleDigit(chromosome);
		}
	}
	
	private void singleDigit(Chromosome chromosome) {
		
		// choose a random digit to change
		int digit = RandomGenerator.nextInt(FindEnsembleMain.NUM_CLASSES);
		
		// set a new index randomly
		chromosome.setValue(digit, RandomGenerator.nextInt(mlpListSize));		
	}
}
